{extend name="../../../view/public/base" /}

{block name="body"}
<div class="bw-container">
    <div class="bw-main">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-collapse" lay-filter="test">
                    <div class="layui-colla-item">
                        <h2 class="layui-colla-title"><i class="layui-icon">&#xe615;</i>查询条件</h2>
                        <div class="layui-colla-content"  >



                            <br>

                            <div class="demoTable layui-form ">

                                <div class="layui-inline">
                                    <label class="layui-form-label">数据组ID：</label>
                                    <div class="layui-inline">
                                        <input class="layui-input" type="number"  name="id" id="id" autocomplete="off" placeholder="数据组ID">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">组名称：</label>
                                    <div class="layui-inline">
                                        <input class="layui-input"  name="name" id="name" autocomplete="off" placeholder="数据组名称">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">组标识：</label>
                                    <div class="layui-inline">
                                        <input class="layui-input"  name="config_name" id="config_name" autocomplete="off" placeholder="数据组标识">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">配置范围：</label>
                                    <div class="layui-input-inline">
                                        <select name="scopes" id="scopes" lay-verify="required" >
                                            <option value="">请选择</option>
                                            <option value="common">平台</option>
                                            <!--<option value="admin">管理员</option>-->
                                            <option value="member">租户</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        &nbsp&nbsp&nbsp
                                        <button class="layui-btn layui-btn-sm   layui-btn-warm" onclick="reload()">搜索</button>
                                        <button class="layui-btn layui-btn-sm" onclick="reset()">重置</button>
                                    </div>
                                </div>

                                <br><br>

                            </div>




                        </div>
                    </div>
                </div>




            </div>
        </div>


        <!--<div class="layui-col-md12">-->
            <!--<div class="layui-card">-->


                <!--<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">-->
                    <!--<ul class="layui-tab-title ">-->
                        <!--<li class="layui-this">全部</li>-->
                        <!--<li>系统配置</li>-->
                        <!--<li>插件配置</li>-->
                        <!--<li>应用配置</li>-->
                        <!--<li>其他配置</li>-->
                    <!--</ul>-->
                <!--</div>-->


            <!--</div>-->
        <!--</div>-->


        <div class="layui-col-md12">
            <div class="layui-card">


                <!-- 列表上面按钮 -->
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container tbtn">
                        <button class="layui-btn layui-btn-danger layui-btn-sm" type="button" onclick="del('all')"><i class="layui-icon">&#xe640;</i>批量删除</button>

                        <button class="layui-btn layui-btn-sm  layui-btn-normal" onclick="edit('','all')">添加数据组</button>
                        <button class="layui-btn layui-btn-sm" onclick="reset()">重置</button>

                    </div>
                </script>


                <!-- 列表 -->
                <table class="layui-table"  lay-data="{toolbar:'#toolbarDemo',url:'{:Url('getconfiglist')}',method:'post',page:true,id:'templateList'}" lay-filter="templateList">
                    <thead>
                    <tr>
                        <th lay-data="{type:'checkbox',fixed: 'left'}"></th>
                        <th lay-data="{field:'id', sort: true}">组ID</th>
                        <th lay-data="{field:'config_name', sort: true}">数据组标识</th>
                        <th lay-data="{field:'name', sort: true}">数据组名称</th>
                        <th lay-data="{field:'scopes', templet: '#scope' }">配置范围</th>
                        <th lay-data="{fixed: 'right', width:300,  align:'center', toolbar: '#barDemo'}">操作</th>
                    </tr>
                    </thead>
                </table>

                <script type="text/html" id="scope">
                    {{#  if(d.scopes == 'common'){ }}<span style="color: #FFB800;">平台</span>{{#  } }}
                    {{#  if(d.scopes == 'admin'){ }}<span style="color: #FF5722;">管理员</span>{{#  } }}
                    {{#  if(d.scopes == 'member'){ }}<span style="color: #1E9FFF;">租户</span>{{#  } }}
                </script>
                <script type="text/html" id="barDemo">
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="edit" title="编辑">编辑</a>
                    {{#  if(d.scopes == 'common'){ }}  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="list" title="数据列标">数据列表</a>  {{#  } }}
                    {{#  if(d.scopes == 'member'){ }}  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="list" title="初始数据">初始数据</a>  {{#  } }}

                    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="list" title="数据列标">数据列表</a>-->
                    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" title="删除">删除</a>

                </script>



            </div>
        </div>
    </div>
    </div>
</div>
<script>

    var table;
    var laydate;
    var form;
    layui.use(['form', 'table','util','laydate'],function() {
        table = layui.table;
        form = layui.form;
        laydate = layui.laydate;
        reset()
        //监听工具条
        table.on('tool(templateList)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                del(data.id);
            } else if (obj.event === 'edit') {
                edit(data.name,data.id);
            }else if (obj.event === 'list') {
                list(data.name,data.id);
            }

        });
        form.render("select");
    });

    //获取所选中的ID
    function getCheckData(){
        var checkStatus = table.checkStatus('templateList'), data = checkStatus.data;
        var obj = [];
        for (var i = 0; data.length > i; i++) {
            obj.push(data[i].id);
        }
        layer.alert(JSON.stringify(obj));
    }
    //获取重载列表
    function reload(){
        // var beginTime = $("#beginTime").val();
        // var endTime = $("#endTime").val();
        var name = $('#name').val();
        var id = $('#id').val();
        var config_name = $('#config_name').val();
        var scopes = $('#scopes').val();

        // document.getElementById("beginTime").value = '';
        // document.getElementById("endTime").value = '';

        table.reload('templateList', {
            where: { //设定异步数据接口的额外参数
                // ,beginTime: beginTime
                // ,endTime: endTime
                name: name
                ,id: id
                ,scopes:scopes
                ,config_name: config_name
            },page: {curr: 1}
        });
    }
    //重置
    function reset(){
        document.getElementById("name").value = '';
        document.getElementById("id").value = '';
        document.getElementById("config_name").value = '';
        document.getElementById("scopes").value = '';
        //执行清空
        // $("#templateType").empty();
        form.render("select");
        //render();
        table.reload('templateList', {
            where: { //设定异步数据接口的额外参数
                // ,beginTime: beginTime
                // ,endTime: endTime
                name: null
                ,id: null
                ,scopes:null
                ,config_name: null
            },page: {curr: 1}
        });
    }

    //新增或修改
    function edit(version,id){
        var currentPage = $('.layui-laypage-em').next().text();
        var title = version + "查看";
        var url = "{:Url('edit')}?id="+id;
        var post_url = "{:Url('edit')}";
        if(id=='all'){
            title ='新增';
            url = "{:Url('add')}";
            post_url = "{:Url('add')}";
        }
        layer.open({
            type: 2,
            title: title,
            shade: 0.2,
            maxmin: true,
            area: ['90%','90%'],
            content: url,
            btn: ['立即提交'],
            yes: function(index, layero){

                var nodeName = window["layui-layer-iframe" + index];
                var id = nodeName.getId();
                var Data = nodeName.getSaveData();
                isClick = false;
                if(Data!= false){
                    $.ajax({
                        url: post_url,
                        type:"post",
                        data:Data,
                        success: function (data) {
                            //console.log(data.status);
                            if(data.data.errcode == 0){
                                layer.close(index);
                                layer.msg('处理中...',{shade:[0.2, '#393D49'],time:60*60*2000});
                                layer.msg('提交成功', {
                                    icon: 6,
                                    time: 1000 //1.5秒关闭（如果不配置，默认是3秒）
                                });
                                setTimeout(function(){
                                    isClick = true;
                                    layer.close(index);
                                    if(id != null && id != ''){
                                        curr = currentPage;
                                        reload();
                                    }else{
                                        reset();
                                    }
                                },1500);
                            }else if(data.data.errcode != 0){
                                layer.msg(data.msg, {
                                    icon: 5,
                                    time: 1500 //1.5秒关闭（如果不配置，默认是3秒）
                                });
                                isClick = true;
                            }else{
                                layer.msg('操作失败！请稍后再试。', {
                                    icon: 5,
                                    time: 1500 //1.5秒关闭（如果不配置，默认是3秒）
                                });
                                isClick = true;
                            }
                        }
                    });
                }
            }
        });
    }

    function getLocalTime(nS) {
        return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
    }

    var isClick = true;
    //删除
    function del(id){
        var ids = new Array();
        if(id == 'all'){
            var checkStatus = table.checkStatus('templateList');
            var data = checkStatus.data;
            if(data == 0){
                layer.alert('请选择');
                return;
            }
            for (var i = 0; i < data.length; i++) {
                ids[i] = data[i].id;
            }
        }else{
            ids[0] = id;
        }
        if(isClick){
            layer.confirm('真的要删除么', function(index) {
                isClick = false;
                layer.close(index);
                $.ajax({
                    url: "{:Url('configsoftdleting')}",
                    type:"post",
                    data:{"ids":ids.toString()},
                    success: function (data) {
                        layer.close(index);
                        if(data == 'success'){
                            layer.msg('删除成功', {
                                icon: 6,
                                time: 1500 //1.5秒关闭（如果不配置，默认是3秒）
                            });
                            setTimeout(function(){
                                isClick = true;
                                reload();
                            },1500);
                        }else if(data == 'nullValueFail'){
                            isClick = true;
                            layer.msg('操作失败！请联系管理员。', {
                                icon: 5,
                                time: 1500 //1.5秒关闭（如果不配置，默认是3秒）
                            });
                        }else if(data == 'paramFail'){
                            isClick = true;
                            layer.msg('操作失败！请删除分类下所有配置再重试。', {
                                icon: 5,
                                time: 1500 //1.5秒关闭（如果不配置，默认是3秒）
                            });
                        }else{
                            isClick = true;
                            layer.msg('未知错误。', {
                                icon: 5,
                                time: 1500 //1.5秒关闭（如果不配置，默认是3秒）
                            });
                        }
                    },error : function(){

                    }
                });
            });
        }
    }

    function list(name,id) {
        layer.open({
            type: 2,
            title: name+'数据列标',
            shade: 0.2,
            maxmin: true,
            area: ['90%','90%'],
            content: "GroupData/"+id,
            //btn: ['立即提交'],
            yes: function(index, layero){


            }
        })

    }

    function HTMLDecode(text) {
        var temp = document.createElement("div");
        temp.innerHTML = text;
        var output = temp.innerText || temp.textContent;
        temp = null;
        return output;
    }
</script>
{/block}